package com.liebaut.controller.order;

import com.liebaut.controller.CrudController;
import com.liebaut.dao.order.DeliveryOrderRepository;
import com.liebaut.dao.order.InvoiceRepository;
import com.liebaut.dao.order.PurchaseOrderRepository;
import com.liebaut.model.order.DeliveryOrder;
import com.liebaut.model.order.Invoice;
import com.liebaut.model.order.Order;
import com.liebaut.model.order.PurchaseOrder;
import com.liebaut.services.IDGeneratorService;
import org.codehaus.jackson.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: User
 * Date: 25/01/14
 * Time: 10:56
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/invoice")
public class InvoiceController extends CrudController<Invoice, Long> {
    private static final Logger log = LoggerFactory.getLogger(InvoiceController.class);

    @Autowired
    IDGeneratorService idGeneratorService;

    @Autowired
    DeliveryOrderRepository deliveryOrderRepository;

    @Autowired
    PurchaseOrderRepository purchaseOrderRepository;

    @Autowired
    public InvoiceController(InvoiceRepository repository) {
        super(repository);
    }

    public InvoiceController() {
    }

    @Override
    public Object save(@RequestBody String body) throws IOException {
        Invoice invoice;
        try {
            invoice = saveRequestToEntity(body);
        } catch (JsonProcessingException e) {
            log.debug("invalid request", e);
            throw new HttpMessageNotReadableException("Invalid request", e);
        }
        Order order = invoice.getOrder();
        if (order instanceof DeliveryOrder) {
            DeliveryOrder deliveryOrder = deliveryOrderRepository.findOne(order.getId());
            deliveryOrder.setDateInvoiced(new Date());
            deliveryOrder.setInvoiced(true);
            deliveryOrderRepository.save(deliveryOrder);
        } else if (order instanceof PurchaseOrder) {
            PurchaseOrder purchaseOrder = purchaseOrderRepository.findOne(order.getId());
            purchaseOrder.setDateInvoiced(new Date());
            purchaseOrder.setInvoiced(true);
            purchaseOrderRepository.save(purchaseOrder);
        }
        repository.save(invoice);
        return invoice;
    }

    @Override
    protected Invoice saveRequestToEntity(String body) throws IOException {
        Invoice invoice = super.saveRequestToEntity(body);
        invoice.setInvoiceNumber(idGeneratorService.getNextNumber("invoice"));
        return invoice;
    }
}
